// pages/user/user.js
let api = require('../../../api/api.js')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navbar: [{
      name: '用户明细',
    }, {
      name: '订单明细',
    }, ],
    oaderTab: [{
        name: '已支付',
      },
      {
        name: '已完成',
      },
      {
        name: '已发货',
      },
    ],
    currentTabsIndex: 0, //  用户明细 默认
    currentTabsIndex_1: 0, //  待配送   默认
    result: [],
    hide: true, //  初始值
    isSearch:true,
    isUserSearch:true
  },
  // 获取焦点
  foucus: function(e) {
    this.setData({
      hide: false
    })
  },
  // 失去焦点
  blur: function(e) {
    this.setData({
      hide: true
    })
  },
  // 图标 X 点击事件
  clear: function() {
    var data = this.data.data
    var param = {}
    param.id = data.id
    param.type = data.type
    api.users_details(param, res => {
      console.log(res)
      this.setData({
        indexData: res.data.result
      })
    })
    this.setData({
      searchValue: "",
      indexData: this.data.result
    })
  },
  clear1:function(){
    var data = this.data.data
    var content = []
    var param = {}
    param.id = data.id
    param.type = data.type
    
    api.userorder(param, res => {
      console.log(res)
      var result = res.data.result
      if (result == "") {
        this.setData({
          indexData: [],
          content: []
        })
      } else {
        result.forEach(function (element) {
          element.user_mobile = element.user_mobile.substr(0, 3) + "******" + element.user_mobile.substr(8)
          if(element.label == 4){
            element.label = "小瓶一提装"
          }
          if (element.label == 6) {
            element.label = "大瓶一箱装"
          }
          if (element.label == 10) {
            element.label = "小瓶一箱装"
          }
          if (element.label == 40) {
            element.label = "小瓶一箱装"
          }
          if (element.order_status == 2) {
            content.push(element)
          }

        })
        this.setData({
          indexData: content,
          content: result
        })
      }
      this.setData({
        currentTabsIndex_1: 0,
        searchValue:"",
        isSearch:true
      })
    })
  },
  confirm1:function(){
    var data = this.data.data
    var content = []
    var param = {}
    param.id = data.id
    param.type = data.type
    api.userorder(param, res => {
      console.log(res)
      var result = res.data.result
      if (result == "") {
        this.setData({
          indexData: [],
          content: []
        })
      } else {
        result.forEach(function (element) {
          element.user_mobile = element.user_mobile.substr(0, 3) + "******" + element.user_mobile.substr(8)
          if (element.label == 4) {
            element.label = "小瓶一提装"
          }
          if (element.label == 6) {
            element.label = "大瓶一箱装"
          }
          if (element.label == 10) {
            element.label = "小瓶一箱装"
          }
          if (element.label == 40) {
            element.label = "小瓶一箱装"
          }
          if (element.order_status == 2) {
            content.push(element)
          }

        })
        this.setData({
          indexData: content,
          content: result
        })
      }
      this.setData({
        currentTabsIndex_1: 0,
        isSearch: true
      })
    })
  },
  // 图标 返回 点击事件
  confirm: function(e) {
    var data = this.data.data
    var param = {}
    param.id = data.id
    param.type = data.type
    api.users_details(param, res => {
      console.log(res)
      this.setData({
        indexData: res.data.result
      })
    })
  },
  // 用户详情 input 点击事件
  search: function(e) {
    var value = e.detail.value
    console.log(value)
    var data = this.data.data
    data.name = value
    api.users_details(data, res => {
      console.log(res)
      this.setData({
        indexData: res.data.result,
        isUserSearch:false
      })
    })
  },
  // 订单明细 input 点击事件
  searchOrder: function(e) {
    var value = e.detail.value
    console.log(e)
    var data = this.data.data
    data.name = value
    var content = []
    api.userorder(data, res => {
      console.log(res)
      var result = res.data.result
      if (result == "") {
        this.setData({
          indexData: [],
          content: []
        })
      } else {
        result.forEach(function (element) {
          element.user_mobile = element.user_mobile.substr(0, 3) + "******" + element.user_mobile.substr(8)
          if (element.label == 4) {
            element.label = "小瓶一提装"
          }
          if (element.label == 6) {
            element.label = "大瓶一箱装"
          }
          if (element.label == 10) {
            element.label = "小瓶一箱装"
          }
          if (element.label == 40) {
            element.label = "小瓶一箱装"
          }
          if (element.order_status == 2) {
            content.push(element)
          }

        })
        this.setData({
          indexData: content,
          content: result
        })
      }
      // result.forEach(function(element){
      //   element.user_mobile = element.user_mobile.substr(0, 3) + "******" + element.user_mobile.substr(8)
      // })
      // this.setData({
      //   indexData: result,
      //   content: result
      // })
      this.setData({
        isSearch:false
      })
    })
  },
  // 用户明细 点击事件
  clickTab: function(e) {
    wx.showLoading({
      title: "加载中"
    })
    setTimeout(function () {
      wx.hideLoading()
    }, 2000)
    console.log(e)
    
    var index = e.currentTarget.dataset.index
    if (index == 0){
      var param = {}
      param.id = this.data.data.id
      param.type = this.data.data.type
      param.mobile = this.data.data.mobile
      param.level = this.data.data.level
      api.users_details(param, res => {
        console.log(res)
        this.setData({
          indexData: res.data.result
        })
      })
    } 
    if (index == 1){
      var content = []
      var param = {}
      param.id = this.data.data.id
      param.type = this.data.data.type
      param.mobile = this.data.data.mobile
      param.level = this.data.data.level
      api.userorder(param, res => {
        console.log(res)
          this.setData({
          indexData: res.data.result
        })
      })
    
      // var content = []
      // var param = {}
      // param.id = this.data.data.id
      // param.type = this.data.data.type
      // param.mobile = this.data.data.mobile
      // param.level = this.data.data.level
      // api.userorder(param, res => {
      //   console.log(res)

      
      //   var result = res.data.result
      //   if(result == ""){
      //     this.setData({
      //       indexData: [],
      //       content: []
      //     })
      //   }else{
      //     result.forEach(function (element) {
      //       element.user_mobile = element.user_mobile.substr(0, 3) + "******" + element.user_mobile.substr(8)
      //       if (element.label == 4) {
      //         element.label = "小瓶一提装"
      //       }
      //       if (element.label == 6) {
      //         element.label = "大瓶一箱装"
      //       }
      //       if (element.label == 10) {
      //         element.label = "小瓶一箱装"
      //       }
      //       if (element.label == 40) {
      //         element.label = "小瓶一箱装"
      //       }
      //       if (element.order_status == 2) {
      //         content.push(element)
      //       }

      //     })
      //     this.setData({
      //       indexData: content,
      //       content: result
      //     })
      //   }
      //   this.setData({
      //     currentTabsIndex_1:0
      //   })
      // })
    }

    var index = e.currentTarget.dataset.index
    this.setData({
      currentTabsIndex: index
    })
  },
  
  // 待配送 点击事件
  clickTab_1: function(e) {
    console.log(e)
    var index_1 = e.currentTarget.dataset.index_1
    var result = this.data.content
    console.log(result)
    var content = []
    if (index_1 == 0){
      console.log("----0----")
      result.forEach(function(element){
        if (element.order_status == '2'){
          content.push(element)
        }
      })
    } else if (index_1 == 1){
      console.log("----1----")
      result.forEach(function (element) {
        if (element.order_status == '3') {
          content.push(element)
        }
      })
    } else if (index_1 == 2) {
      console.log("----2----")
      result.forEach(function (element) {
        if (element.order_status == '5') {
          content.push(element)
        }
      })
    }
    console.log(content)
    this.setData({
      currentTabsIndex_1: index_1,
      indexData: content
    })
  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(data) {
    console.log(data)
    wx.showLoading({
      title: "加载中"
    })
    setTimeout(function() {
      wx.hideLoading()
    }, 2000)

    api.users_details(data, res => {
      console.log(res)
      // 动态显示页头标题。
      // wx.setNavigationBarTitle({
      //   title: res.data.result.name
      // })
      this.setData({
        indexData: res.data.result
      })
    })
    this.setData({
      data: data
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})